#include "MyEmotiv.h"


MyEmotiv::MyEmotiv(void)
{
	Instrument::Instrument();
	
	eEvent	= EE_EmoEngineEventCreate();
	eState	= EE_EmoStateCreate();
	composerPort = 1726;
	userID	= 0;
	datarate = 0;
	readytocollect = false;
	option	= 0;
	state	= 0;

	// 16bit
	DigitalOffset = 8192/2;
	DigitalMax = 8192;
	DigitalMin = 0;
	Resolution = 8.3880f/8192.0f;
	
		
	//Set the buffer lenght in seconds
	secs = 0.1;
	
	numCh = 0;

	ConfigDiag = NULL;
	hData = NULL;
	configured = false;
	
	
	Configure();
	MyName = _("MyEmotiv");
}

MyEmotiv::~MyEmotiv(void)
{
	
}

void* MyEmotiv::Entry(void)
{
	bool		SynchOk = false;
	int			Dim;
	int			read;
	char		Buffer[16];//,2*ch;
	SampleStruct	Signal;
	memset(&Signal,0,sizeof(Signal));
	Signal.Data.DataLen	=	Channels;
	int cont;

	Esci = false;
	while(!Esci)
	{
		if(IsRunning)
		{
			EE_DataUpdateHandle(0, hData);
			unsigned int nSamplesTaken=0;
			EE_DataGetNumberOfSample(hData,&nSamplesTaken);
		
			if (nSamplesTaken != 0) 
			{
				double* data = new double[nSamplesTaken];
				for (int sampleIdx=0 ; sampleIdx<(int)nSamplesTaken ; ++ sampleIdx) 
				{
					cont = 0;
					for (int i = 0 ; i<sizeof(targetChannelList)/sizeof(EE_DataChannel_t) ; i++) 
					{
						EE_DataGet(hData, targetChannelList[i], data, nSamplesTaken);
						//Read only the selected data
						if ((i>0) && (i<=14))
							if (MyChannel[i-1] == 1)
							{
								Signal.Data.Signal[cont] = *data;
								cont++;
							}
						
						if ((i>14) && (i<=16))
							if (MyChannel[i-1] == 1)
							{
								Signal.Data.Signal[cont] = (*data-658)*4;
								cont++;
							}
					}	
					PutSample(&Signal);
				}
			}
			else
				Sleep(10);
		}
	}
	if (hData!=NULL)
		EE_DataFree(hData);
	StopDevice();
	Sleep(100);
	

	return NULL;
}

void MyEmotiv::StartDevice(wxString FileName,wxString FilePath,bool File)
{
	if (!IsRunning)
	{
		Instrument::StartDevice(FileName,FilePath,File);
		hData = EE_DataCreate();
		EE_DataSetBufferSizeInSec(secs);

		state = EE_EngineGetNextEvent(eEvent);
		if (state == EDK_OK) 
		{
			EE_Event_t eventType = EE_EmoEngineEventGetType(eEvent);
			EE_EmoEngineEventGetUserId(eEvent, &userID);

			if (eventType == EE_UserAdded) 
			{
				//Enable the acquisition.
				EE_DataAcquisitionEnable(userID,true);
				IsRunning = true;
			}
		}
	}
}

void MyEmotiv::StopDevice(void)
{
	if(IsRunning)
	{
		IsRunning	=	false;
		EE_EngineDisconnect();
		EE_EmoStateFree(eState);
		EE_EmoEngineEventFree(eEvent);
		Instrument::StopDevice();
	}
}

int MyEmotiv::OpenPort(const char *PortName, const char *OpenVal2, const char *OpenVal3)
{
	if (EE_EngineConnect() != EDK_OK)
	//if (EE_EngineRemoteConnect("127.0.0.1",1726) != EDK_OK)
		return -1;
	else
		return 1;
}

void MyEmotiv::ChannelsInit (int numCh)
{
	targetChannelList[0] = ED_COUNTER;
	targetChannelList[1] = ED_AF3;
	targetChannelList[2] = ED_F7;
	targetChannelList[3] = ED_F3;
    targetChannelList[4] = ED_FC5;
	targetChannelList[5] = ED_T7;
	targetChannelList[6] = ED_P7;
	targetChannelList[7] = ED_O1;
	targetChannelList[8] = ED_O2;
	targetChannelList[9] = ED_P8;
	targetChannelList[10] = ED_T8;
	targetChannelList[11] = ED_FC6;
	targetChannelList[12] = ED_F4;
	targetChannelList[13] = ED_F8;
	targetChannelList[14] = ED_AF4;
	targetChannelList[15] = ED_GYROX;
	targetChannelList[16] = ED_GYROY;
	targetChannelList[17] = ED_TIMESTAMP;
	targetChannelList[18] = ED_FUNC_ID;
	targetChannelList[19] = ED_FUNC_VALUE;
	targetChannelList[20] = ED_MARKER;
	targetChannelList[21] = ED_SYNC_SIGNAL;

	int cont=0;
	if (MyChannel[0])
	{
		Labels[cont] = wxT("AF3");
		cont++;
	}
	if (MyChannel[1])
	{
		Labels[cont] = wxT("F7");
		cont++;
	}
	if (MyChannel[2])
	{
		Labels[cont] = wxT("F3");
		cont++;
	}
	if (MyChannel[3])
	{
		Labels[cont] = wxT("FC5");
		cont++;
	}
	if (MyChannel[4])
	{
		Labels[cont] = wxT("T7");
		cont++;
	}
	if (MyChannel[5])
	{
		Labels[cont] = wxT("P7");
		cont++;
	}
	if (MyChannel[6])
	{
		Labels[cont] = wxT("O1");
		cont++;
	}
	if (MyChannel[7])
	{
		Labels[cont] = wxT("O2");
		cont++;
	}
	if (MyChannel[8])
	{
		Labels[cont] = wxT("P8");
		cont++;
	}
	if (MyChannel[9])
	{
		Labels[cont] = wxT("T8");
		cont++;
	}
	if (MyChannel[10])
	{
		Labels[cont] = wxT("FC6");
		cont++;
	}
	if (MyChannel[11])
	{
		Labels[cont] = wxT("F4");
		cont++;
	}
	if (MyChannel[12])
	{
		Labels[cont] = wxT("F8");
		cont++;
	}
	if (MyChannel[13])
	{
		Labels[cont] = wxT("AF4");
		cont++;
	}
	if (MyChannel[14])
	{
		Labels[cont] = wxT("GyroX");
		cont++;
	}
	if (MyChannel[15])
	{
		Labels[cont] = wxT("GyroY");
		cont++;
	}
	//P3 and P4 are the references

	for(int i=0;i<cont;i++)
	{
		Maximum[i] = DigitalOffset*Resolution;
		Minimum[i] = -DigitalOffset*Resolution;
	}
	
		

}

void MyEmotiv::Configure(void)
{
	if (ConfigDiag == NULL)
	{
		ConfigDiag = new ConfigEmoDiag(NULL,MyChannel,&configured);
		ConfigDiag->ShowModal();

		delete(ConfigDiag);

		if(configured)
		{
			Esci = true;
			for (int i=0; i<16; i++)
			{	
				if (MyChannel[i] == 1)
					numCh++;
			}
			ChannelsInit(numCh);
			Initialize(numCh,128,20);
			
		}
		else
		{
			//This initialize the use of all the channels
			for (int i=0; i<16; i++)
				MyChannel[i] = 1;
			numCh = 16;
			ChannelsInit(numCh);
			Initialize(numCh,128,20);
		}
	}
}
